package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.ShouCangListTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2016/3/20.
 */
public class MyShouCangListActivity extends BaseActivity {
    private Context mContext;
    private PullToRefreshGridView mListView;
    private VideoAdapter mAdapter;
    private int moreVideoTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private TextView back;
    private LinearLayout mEmptyView;

    private LinearLayout mEmptyContentView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoucanglist);
        mContext = this;
        initView();


    }

    private void initView() {
        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        back= (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshGridView) findViewById(R.id.shoucang_list);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage =1;
                getMoreVieoList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                getMoreVieoList();
            }
        });
        mAdapter = new VideoAdapter(mContext);
        mListView.setAdapter(mAdapter);
        getMoreVieoList();
    }

    private void  getMoreVieoList(){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//            ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if(moreVideoTaskId != -1){
            return;
        }
        ShouCangListTask task = new ShouCangListTask(size,currentPage);
        task.addListener(new MoreListner(mContext));
        moreVideoTaskId = getEatShowApp().getTaskManager().addTask(task);

    }


    class MoreListner extends ShowToastTaskEventListener {

        public MoreListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if(data != null){
                    attentionList = (List<VideoInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                if(currentPage ==1 && mAdapter.getCount()>0){
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }

                currentPage = result;
//                if (currentPage ==0){
//                    mListView.getLoadingLayoutProxy(false,true).setReleaseLabel(getString(R.string.no_more));
//                    mListView.getLoadingLayoutProxy(false,true).setPullLabel(getString(R.string.no_more));
//                    mListView.getLoadingLayoutProxy(false,true).setRefreshingLabel(getString(R.string.no_more));
//                }
                if(mAdapter != null && mAdapter.getCount() == 0){
                    EmptyDataUtil.updateEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == moreVideoTaskId){
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
            }
        }
    }

//    public void onHiddenChanged(boolean hidden) {
//        onHiddenChanged(hidden);
//        if (!hidden) {
//            mAdapter.clearData();
//           getMoreVieoList();
//        }
//    }


    @Override
    protected void onResume() {
        super.onResume();
//        mAdapter.clearData();
        currentPage = 1;
        getMoreVieoList();
    }
}
